package com.klhappy.houyuan.adapter;

import java.util.ArrayList;
import java.util.List;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.activity.OrderDetailActivity;
import com.klhappy.houyuan.activity.SendDetialActicity;
import com.klhappy.houyuan.bean.GoodsAllBean;
import com.klhappy.houyuan.bean.SendBean;
import com.klhappy.houyuan.activity.ConfirmOrderActivity;
import com.klhappy.houyuan.fragement.dingdanfragment.SendFragment;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.UnfoldedListView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class SendAdapter extends BaseAdapter {

    private Context context;
    private List<SendBean.Records> records;
    private ArrayList<SendBean.OrderDetail> orderDetail;
    private ArrayList<GoodsAllBean> itemList;

    public SendAdapter(Context context, List<SendBean.Records> records) {
        this.context = context;
        this.records = records;
        itemList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return records.size();
    }

    @Override
    public Object getItem(int position) {
        return records.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_send, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        orderDetail = (ArrayList<SendBean.OrderDetail>) JsonCode.getClassList(records.get(position).getOrder_detail(), SendBean.OrderDetail.class);
        String time = records.get(position).getOr_pub_date() + "" + records.get(position).getDays();
        String status = records.get(position).getSta_desc();
        String sum = "共" + orderDetail.size() + "样商品";
        String amount = "合计:  ￥" + records.get(position).getOr_amount();
        String free = "(派送费:  " + records.get(position).getLog_free() + ")";

        holder.tvTime.setText(time);
        holder.tvStatus.setText(status);
        holder.tv_sum.setText(sum);
        holder.tv_amount.setText(amount);
        holder.tv_free.setText(free);
        holder.lvContent.setAdapter(new SendGoodsAdapter(context, orderDetail));

        holder.btn_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orderDetail = (ArrayList<SendBean.OrderDetail>) JsonCode.getClassList(records.get(position).getOrder_detail(), SendBean.OrderDetail.class);
                String time = records.get(position).getOr_pub_date() + "" + records.get(position).getDays();
                itemList.clear();
                for (int i = 0; i < orderDetail.size(); i++) {
                    itemList.add(new GoodsAllBean(orderDetail.get(i).getGoods_id(), orderDetail.get(i).getG_icon()
                            , orderDetail.get(i).getG_name()
                            , orderDetail.get(i).getG_price()
                            , orderDetail.get(i).getG_unit()
                            , orderDetail.get(i).getNo(), time));
                }
                Intent intent = new Intent(context, ConfirmOrderActivity.class);
                intent.putExtra("goods", itemList);
                context.startActivity(intent);
            }
        });
        holder.lvContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int j, long id) {
                orderDetail = (ArrayList<SendBean.OrderDetail>) JsonCode.getClassList(records.get(position).getOrder_detail(), SendBean.OrderDetail.class);
                itemList.clear();
                String time = records.get(position).getOr_pub_date() + "" + records.get(position).getDays();
                for (int i = 0; i < orderDetail.size(); i++) {
                    itemList.add(new GoodsAllBean(orderDetail.get(i).getGoods_id(), orderDetail.get(i).getG_icon()
                            , orderDetail.get(i).getG_name()
                            , orderDetail.get(i).getG_price()
                            , orderDetail.get(i).getG_unit()
                            , orderDetail.get(i).getNo(), time));
                }
                Intent intent = new Intent(context, SendDetialActicity.class);
                intent.putExtra("order", itemList);
                intent.putExtra("or_id", records.get(position).getOr_id());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.tv_time)
        TextView tvTime;
        @InjectView(R.id.tv_status)
        TextView tvStatus;
        @InjectView(R.id.tv_sum)
        TextView tv_sum;
        @InjectView(R.id.tv_amount)
        TextView tv_amount;
        @InjectView(R.id.tv_free)
        TextView tv_free;
        @InjectView(R.id.btn_look)
        Button btn_look;
        @InjectView(R.id.lv_content)
        UnfoldedListView lvContent;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
